%{
	#include <stdio.h>
	#include <stdlib.h>

	FILE *outputFile;
%}

%%
digit		[0-9]
letter		[a-zA-Z]

IDENFR	    ({letter}|_)({letter}|{digit}|_)*
INTCON  	{digit}+
MAINTK      "main"
STRCON      ["]([^\\\"]|\\.)*["]
CONSTTK     "const"
INTTK       "int"
BREAKTK     "break"
CONTINUETK  "continue"
IFTK        "if"
ELSETK      "else"
NOT         "!"
AND         "&&"
OR          "||"
WHILETK     "while"
GETINTTK    "getint"
PRINTFTK    "printf"
RETURNTK    "return"
PLUS        "+"
MINU        "-"
VOIDTK      "void"
MULT        "*"
DIV         "/"
MOD         "%"
LSS         "<"
LEQ         "<="
GRE         ">"
GEQ         ">="
EQL         "=="
NEQ         "!="
ASSIGN      "="
SEMICN      ";"
COMMA       ","
LPARENT     "("
RPARENT     ")"
LBRACK      "["
RBRACK      "]"
LBRACE      "{"
RBRACE      "}"

other		.

%%
{IDENFR}            { fprintf("IDENFR %s)\n",yytext); }
{INTCON}            { fprintf("INTCON %s)\n",yytext); }
{STRCON}            { fprintf("STRCON %s)\n",yytext); }
{MAINTK}            { fprintf("MAINTK %s)\n",yytext); }
{CONSTTK}           { fprintf("CONSTTK %s)\n",yytext); }
{INTTK}             { fprintf("INTTK %s)\n",yytext); }
{BREAKTK}           { fprintf("BREAKTK %s)\n",yytext); }
{CONTINUETK}        { fprintf("CONTINUETK %s)\n",yytext); }
{IFTK}              { fprintf("IFTK %s)\n",yytext); }
{ELSETK}            { fprintf("ELSETK %s)\n",yytext); }
{NOT}               { fprintf("NOT %s)\n",yytext); }
{AND}               { fprintf("AND %s)\n",yytext); }
{OR}                { fprintf("OR %s)\n",yytext); }
{WHILETK}           { fprintf("WHILETK %s)\n",yytext); }
{GETINTTK}          { fprintf("GETINTTK %s)\n",yytext); }
{PRINTFTK}          { fprintf("PRINTFTK %s)\n",yytext); }
{RETURNTK}          { fprintf("RETURNTK %s)\n",yytext); }
{PLUS}              { fprintf("PLUS %s)\n",yytext); }
{MINU}              { fprintf("MINU %s)\n",yytext); }
{VOIDTK}            { fprintf("VOIDTK %s)\n",yytext); }
{MULT}              { fprintf("MULT %s)\n",yytext); }
{DIV}               { fprintf("DIV %s)\n",yytext); }
{MOD}               { fprintf("MOD %s)\n",yytext); }
{LSS}               { fprintf("LSS %s)\n",yytext); }
{LEQ}               { fprintf("LEQ %s)\n",yytext); }
{GRE}               { fprintf("GRE %s)\n",yytext); }
{GEQ}               { fprintf("GEQ %s)\n",yytext); }
{EQL}               { fprintf("EQL %s)\n",yytext); }
{NEQ}               { fprintf("NEQ %s)\n",yytext); }
{ASSIGN}            { fprintf("ASSIGN %s)\n",yytext); }
{SEMICN}            { fprintf("SEMICN %s)\n",yytext); }
{COMMA}             { fprintf("COMMA %s)\n",yytext); }
{LPARENT}           { fprintf("LPARENT %s)\n",yytext); }
{RPARENT}           { fprintf("RPARENT %s)\n",yytext); }
{LBRACK}            { fprintf("LBRACK %s)\n",yytext); }
{RBRACK}            { fprintf("RBRACK %s)\n",yytext); }
{LBRACE}            { fprintf("LBRACE %s)\n",yytext); }
{RBRACE}            { fprintf("RBRACE %s)\n",yytext); }

{other}		{printf("illegal character:\'%s\'\n",yytext);}

%%
int main(){
	FILE *outputFile;
    outputFile = fopen("output.txt", "w");
    if (outputFile == NULL) {
        fprintf(stderr, "Cannot open output file.\n");
        return 1;
    }

    yyin = fopen("data.txt", "r");
    if (yyin == NULL) {
        fprintf(stderr, "Cannot open input file.\n");
        return 1;
    }

    yylex(); // 调用词法分析器处理输入文件

    fclose(yyin);
    fclose(outputFile);

    return 0;
}

int yywrap(){
	return 1;
}








([a-zA-Z]|_)([a-zA-Z]|[0-9]|_)*         { fprintf(outputFile, "IDENFR %s)\n",yytext); }

.                                       {printf("illegal character:\'%s\'\n",yytext);}